/**
 *  NetLogo Parameter Hash Map
 *
 * 
 *   @author NetLogo
 */



package model;

import java.util.ArrayList;
import java.util.HashMap;

public class ParameterHashMap extends HashMap<String, ParameterSpec>  {
	private static final long serialVersionUID = 1L;
	
    public String addParamSpec(String paramString)
    {
            // make sure there's enough white space around everything
            String ps = paramString.replaceAll( "\\[" , " [ " );
            ps = ps.replaceAll( "\\]" , " ] " );
            java.util.Scanner scanner = new java.util.Scanner(ps);
            ParameterSpec pspec;

            if (!scanner.next().equals( "[" ))
            {
                    throw new IllegalArgumentException("Invalid parameter spec: " + paramString);
            }
            String name = scanner.next();
            String tok = scanner.next();
            if (tok.equals("[")) // ranged specification
            {
                    double dMin = Double.parseDouble( scanner.next() );
                    tok = scanner.next();
                    double dMaxOrStep = Double.parseDouble(tok);
                    tok = scanner.next();                    
                    if (tok.equals("]")) // continuous
                    {
                            pspec = new DoubleLevelSpec(name, dMin, dMaxOrStep);
                    }
                    else
                    {

                            double dMax = Double.parseDouble( tok );
                            pspec = new DoubleDiscreteSpec(name, dMin, dMaxOrStep, dMax);
                    }
                    //if (!scanner.next().equals( "]" ))
                    //{
                    //        throw new IllegalArgumentException("Invalid parameter spec: " + paramString);
                    //}
            }
            else
            {
                    ArrayList<Object> vals = new ArrayList<Object>();
                    while (!tok.equals( "]" ) && scanner.hasNext())
                    {
                            vals.add( nlStrToObj(tok) );
                            tok = scanner.next();
                    }
                    if (!tok.equals("]"))
                    {
                            throw new IllegalArgumentException("Invalid parameter spec: " + paramString);
                    }
                    if (vals.size() == 1)
                    {
                            pspec = new ConstantSpec(name, vals.get( 0 ));
                    }
                    else
                    {
                            pspec = new CategoricalSpec(name, vals);
                    }
            }
            System.out.println(pspec);
            System.out.println(((Object)pspec).getClass());
            // Commented from forest
            put(pspec.name, pspec);
            System.out.println("name2" + pspec.name);
            return pspec.name; 
    }
    
	// TODO: We should probably use the NetLogo interpreter instead,
	//       so we can properly handle e, pi, colors, etc...
	private static Object nlStrToObj(String s)
	{
		if (s.equalsIgnoreCase( "true" ))
			return Boolean.TRUE;
		else if (s.equalsIgnoreCase( "false" ))
			return Boolean.FALSE;
		else if (s.startsWith( "\"" ) && s.endsWith( "\""))
			return s.substring(1,s.length()-1);
		else
		{
			return Double.parseDouble( s );	
		}			
	}
	
    public static void main (String[] args) {
    	ParameterHashMap paramterData = new ParameterHashMap();
    	paramterData.addParamSpec("[test1 1 2 3]");
    	paramterData.addParamSpec("[test2 \"a\" \"b\" \"c\"]");
    	paramterData.addParamSpec("[test3 [1 2 3]]");
    	paramterData.addParamSpec("[test4 [1 2 3]]");
    	paramterData.addParamSpec("[test5 1]");
    }   
	
	
}
